package com.xspace.android.xafcommon.db.dbsub;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xspace.android.xafcommon.been.WorkItem;
import com.xspace.android.xafcommon.db.BaseSql;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * liuchen 创建于 2016/9/20.
 */
public class WorkTypeDBBiz extends BaseSql {

    private static final String TAG = WorkTypeDBBiz.class.getSimpleName();

    private String tableName = "WorkType";
    private SQLiteDatabase sqldb;
    private MyDBHelper helpDb;
    private String WorkType = "work_type";
    private String WorkTypeName = "work_typename";
    private String Table_name = "table_name";
    private String ApplyFormKey = "apply_form_key";
    private String ApplyFormId = "apply_form_id";
    private String WorkId = "work_id";
    //存放userId
    private String Remark1 = "remark1";
    //存放schedule_id
    private String Remark2 = "remark2";
    private String Remark3 = "remark3";

    protected WorkTypeDBBiz() {
        super();

    }

    public WorkTypeDBBiz(MyDBHelper db) {
        super(db);
        helpDb = db;
    }

    @Override
    public void create(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + tableName + " (_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
                WorkType + "  text," +
                WorkTypeName + " text," +
                Table_name + " text," +
                ApplyFormKey + " text," +
                ApplyFormId + " text ," +
                WorkId + " text ," +
                Remark1 + " text ," +
                Remark2 + " text ," +
                Remark3 + " text );");
    }

    protected void checkDb() {
        if (sqldb == null || !sqldb.isOpen()) {
            sqldb = helpDb.getWritableDatabase();
        }
    }

    /**
     * 查询所有工作事项记录
     *
     * @return
     */
    public List<WorkItem> queryAllRows(String userId) {
        checkDb();
        List<WorkItem> itemList = new ArrayList<WorkItem>();
        selection = Remark1 + "= ?";
        selectionArgs = new String[]{userId};
        String[] columns = {WorkType, WorkTypeName, Table_name, ApplyFormKey, ApplyFormId, WorkId, Remark2};
        Cursor cur = sqldb.query(tableName, columns, selection, selectionArgs, null, null, null);
        if (cur != null) {
            while (cur.moveToNext()) {
                String workType = cur.getString(0);
                String workTypeName = cur.getString(1);
                String tableName = cur.getString(2);
                String formKey = cur.getString(3);
                String formId = cur.getString(4);
                String workId = cur.getString(5);
                String scheduleId = cur.getString(6);
                LogUtil.v(TAG, "queryDataByUserid workType:" + workType + " workTypeName:" + workTypeName);
                WorkItem wi = new WorkItem();
                wi.work_type = workType;
                wi.work_typename = workTypeName;
                wi.table_name = tableName;
                wi.apply_form_key = formKey;
                wi.apply_form_id = formId;
                wi.work_id = workId;
                wi.schedule_id = scheduleId;
                itemList.add(wi);
            }
            cur.close();
        }
        return itemList;
    }

    /**
     * 插入数据
     */
    public void insertOrUpdate(List<WorkItem> dataList, String userId) {
        //清除之前的数据，再次插入新的数据
        selectionArgs = null;
        selection = null;
        checkDb();
        selection = Remark1 + "= ?";
        selectionArgs = new String[]{userId};
        int deleteResult = sqldb.delete(tableName, selection, selectionArgs);
        LogUtil.d(TAG, "deleteResult " + deleteResult);
        values = new ContentValues();
        for (WorkItem workItem : dataList) {
            values.put(Remark1, userId);
            values.put(WorkType, workItem.work_type);
            values.put(WorkId, workItem.work_id);
            values.put(WorkTypeName, workItem.work_typename);
            values.put(Table_name, workItem.table_name);
            values.put(ApplyFormKey, workItem.apply_form_key);
            values.put(ApplyFormId, workItem.apply_form_id);
            values.put(Remark2, workItem.schedule_id);
            where = " 1=2 ";
            LogUtil.d(TAG, "insertOrUpdate " + workItem.work_typename);
//            saveOrUpdate(tableName);
            long result = sqldb.insert(tableName, null, values);
            LogUtil.d(TAG, "insertOrUpdate result " + result);
        }
    }

    @Override
    public void upgrade(SQLiteDatabase db) {

    }

    @Override
    protected synchronized int saveOrUpdate(String tableName) {
        return super.saveOrUpdate(tableName);
    }

    @Override
    protected synchronized int update(String tableName) {
        return super.update(tableName);
    }

    @Override
    protected int delete(String tableName) {
        return super.delete(tableName);
    }

    @Override
    protected int total(String table) {
        return super.total(table);
    }


}
